package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2024-06-04
 * Time: 22:02
 */
public class DelBooks implements IBooks{
    @Override
    public void work(BookList booklist){
        int count = booklist.getUsedSize();
        if(count == 0){
            System.out.println("图书馆没有图书！");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书的名字:>");
        String name = scanner.nextLine();
        int tmp = -1;
        for (int i = 0; i < count; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)){
                tmp = i;
                break;
            }
        }
        if(tmp == -1){
            System.out.println("没有找到该图书，请重新选择！");
            return;
        }else{
            for (int i = tmp; i < count - 1; i++) {
                Book book1 = booklist.getBook(i+1);
                booklist.setBooks(i,book1);
            }
        }
        booklist.setUsedSize(count - 1);
        System.out.println("删除图书成功！");
        return;
    }
}
